package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.GsonUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysStudent;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 学生信息
 * 
 * @author ruoyi
 */
@Slf4j
@Api(tags = "学校学生信息")
@RestController
@RequestMapping("/student")
public class SysStudentController extends BaseController {

    @Autowired
    private ISysStudentService studentService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取学生列表
     */
    @RequiresPermissions("system:student:list")
    @GetMapping("/list")
    public TableDataInfo list(SysStudent user) {
        startPage();
        List<SysStudent> list = studentService.selectStudentList(user);
        return getDataTable(list);
    }

    @Log(title = "学生导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:user:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStudent user) {
        List<SysStudent> list = studentService.selectStudentList(user);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        util.exportExcel(response, list, "学生数据");
    }

    @Log(title = "学生数据导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:user:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        List<SysStudent> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        log.info("导入数据的学生：{}", GsonUtil.toJSONString(userList));
        String message = studentService.importStudent(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 模板下载
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        util.importTemplateExcel(response, "学生数据");
    }

    /**
     * 获取当前学生信息
     */
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<LoginUser> info(@PathVariable("username") String username) {

        log.info("获取当前学生信息");
        return R.ok(null);
    }

    /**
     * 注册学生信息
     */
    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody SysStudent SysStudent) {
        log.info("注册学生信息");
        return R.ok();
    }

    /**
     * 获取学生信息
     * 
     * @return 学生信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        log.info("获取学生信息");
        return null;
    }

    /**
     * 根据学生编号获取详细信息
     */
    @RequiresPermissions("system:user:query")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        log.info("根据学生编号获取详细信息:{}", userId);
        return null;
    }

    /**
     * 新增学生
     */
    @RequiresPermissions("system:user:add")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysStudent user) {
        log.info("新增学生:{}", user);
        return null;
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "学生修改管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysStudent user) {
        log.info("修改学生:{}", user);
        return null;
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("system:user:remove")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        log.info("删除学生:{}", userIds);
        return null;
    }

    /**
     * 重置密码
     */
//    @RequiresPermissions("system:user:edit")
//    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
//    @PutMapping("/resetPwd")
//    public AjaxResult resetPwd(@RequestBody SysStudent user) {
//
//        return null;
//    }

    /**
     * 状态修改
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysStudent user) {
        log.info("删学生状态修改:{}", user);
        return null;
    }

    /**
     * 根据学生编号获取授权角色
     */
//    @RequiresPermissions("system:user:query")
//    @GetMapping("/authRole/{userId}")
//    public AjaxResult authRole(@PathVariable("userId") Long userId) {
//        log.info("根据学生编号获取授权角色:{}", userId);
//        return null;
//    }

    /**
     * 学生授权角色
     */
//    @RequiresPermissions("system:user:edit")
//    @Log(title = "学生管理", businessType = BusinessType.GRANT)
//    @PutMapping("/authRole")
//    public AjaxResult insertAuthRole(Long userId, Long[] roleIds) {
//
//        return success();
//    }

    /**
     * 获取部门树列表
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept) {

        return AjaxResult.success(deptService.selectDeptTreeList(dept));
    }
}
